package dao;

import dbutil.DBHelp;
import vo.Course;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by ssw on 2018/6/12.
 */
public class CourseDao {

    public List<Course> askCourseList() {
        DBHelp dh=new DBHelp();
        List<Course> courseList=new LinkedList<>();
        String sql="select * from course";
        try {
            ResultSet rs=dh.query(sql);
            while(rs.next()){
                courseList.add(new Course(rs.getString(1),rs.getString(2)));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                dh.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return courseList;
    }

    public boolean addCourse(String cname) {
        int temp=-1;
        DBHelp dh=new DBHelp();
        String sql="insert into course(cname) values(?)";
        try {
            temp=dh.insert(sql,cname);
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }finally {
            try {
                dh.close();
            } catch (SQLException e) {
                e.printStackTrace();

            }
        }
        return temp>0;
    }
}
